/*
 * @Descripttion: 
 * @version: 
 * @Author: TT
 * @Date: 2023-05-05 09:45:00
 * @LastEditors: TT
 * @LastEditTime: 2023-05-08 11:54:33
 */

import 'dart:io';

import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:hzy_common_module/utils/image_picker_tool.dart';
import 'package:hzy_common_module/utils/permission_helper.dart';
import 'package:hzy_common_module/utils/utils.dart';
import 'package:hzy_common_module/utils/wechat_picker_tool.dart';
import 'package:hzy_normal_tool/hzy_normal_tool.dart';
import 'package:image_picker/image_picker.dart';
import 'package:permission_handler/permission_handler.dart';
import 'package:wechat_assets_picker/wechat_assets_picker.dart';
import 'package:wechat_camera_picker/wechat_camera_picker.dart';

import 'getx_util_tool.dart';

class PickerTool {
  /*
   * 显示仿微信相册相机功能
   * selectedAssets 选择的图片
   * maxAssets 最多个数
   * requestType: 相册支持的样式 图片 视频
   * pickerConfig 相册配置文件
   * cameraConfig 相机配置文件
   * type: 0 相册 1 相机
   * themeColor 主题色
   * */
  static showBottomSheetWechatPickerTool({
    required BuildContext context,
    List? selectedAssets,
    int maxAssets = 1,
    Function(List<AssetEntity>? list)? onChange,
    RequestType requestType = RequestType.image,
    AssetPickerConfig? pickerConfig,
    CameraPickerConfig cameraConfig = const CameraPickerConfig(),
    Color? themeColor,
  }) {
    List<AssetEntity>? list;
    if (selectedAssets != null && selectedAssets.isNotEmpty) {
      list = selectedAssets.map((e) => e as AssetEntity).toList();
    }
    configShowBottomSheet(
      context: context,
      child: HzyPopSelectImageWidget(
        tapCall: (index) async {
          currentGoback();
          WechatPickerTool.configWechatPickerTool(
            context: context,
            selectedAssets: list,
            maxAssets: maxAssets,
            requestType: requestType,
            pickerConfig: pickerConfig,
            cameraConfig: cameraConfig,
            themeColor: themeColor,
            type: index,
            onChange: onChange,
          );
        },
      ),
    );
  }

  /*
   * 显示官方相册相机功能
   * ImagePicker 选择器
   * isMult 只针对相册生效，是否可以多选 
   * maxWidth: 图片最大宽度
   * maxHeigh: 图片最大高度
   * imageQuality 图像质量 0～1
   * preferredCameraDevice: 摄像头  rear：后置 front：前置 
   * requestFullMetadata： 是否使用原图 默认true
   * type: 0 相册 1 相机
   * */
  static showBottomSheetImagePickerTool({
    required BuildContext context,
    ImagePicker? imagePicker,
    bool isMult = false,
    double? maxWidth,
    double? maxHeight,
    int? imageQuality,
    CameraDevice preferredCameraDevice = CameraDevice.rear,
    bool requestFullMetadata = true,
    Function(List<XFile>? list)? onChange,
  }) {
    ImagePickerTool.configImagePickerTool(
      context: context,
      imagePicker: imagePicker,
      isMult: isMult,
      maxHeight: maxHeight,
      maxWidth: maxWidth,
      type: 0,
      preferredCameraDevice: preferredCameraDevice,
      requestFullMetadata: requestFullMetadata,
      onChange: onChange,
    );

    // configShowBottomSheet(
    //   context: context,
    //   child: HzyPopSelectImageWidget(
    //     tapCall: (index) async {
    //       currentGoback();
    //       ImagePickerTool.configImagePickerTool(
    //         context: context,
    //         imagePicker: imagePicker,
    //         isMult: isMult,
    //         maxHeight: maxHeight,
    //         maxWidth: maxWidth,
    //         type: index,
    //         preferredCameraDevice: preferredCameraDevice,
    //         requestFullMetadata: requestFullMetadata,
    //         onChange: onChange,
    //       );
    //     },
    //   ),
    // );
  }

  /// 配置相册和相机权限
  static configImagePickerPermission({
    required BuildContext context,
    int status = 0,
    Function()? onSuccess,
  }) {
    if (kIsWeb) {
      if (onSuccess != null) {
        onSuccess();
      }
      return;
    }
    Permission permission;
    if (status == 0) {
      if (Platform.isIOS) {
        permission = Permission.photosAddOnly;
      } else {
        permission = Permission.storage;
      }
    } else {
      permission = Permission.camera;
    }
    showload();
    PermissionHelper.check(
      [permission],
      onSuccess: () async {
        if (onSuccess != null) {
          onSuccess();
        }
      },
      onFailed: (per) {
        permissionpopupWidget(
          context: context,
          status: status,
        );
      },
      onOpenSetting: () {
        permissionpopupWidget(
          context: context,
          status: status,
        );
      },
    );
  }

  /*
   * @description: 权限未开通 , 弹框提示,跳转设置界面
   * @param {*}
   * @return {*}
   */
  static permissionpopupWidget(
      {required BuildContext context, required int status}) {
    loaddismiss();
    String msg = "需要打开相册权限才能加载相册哦~";
    if (status == 1) {
      msg = "需要打开相机权限才能拍照哦~";
    }
    showPopDiaLogWidget(
      context: context,
      msg: msg,
      tapSure: () {
        currentGoback();
        openAppSettings();
      },
    );
  }
}
